﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_JobsiteCommands : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private ICommandBLL cmdBLL = new CommandBLL();
        private IJobSiteCommandBLL oop = new JobSiteCommandBLL();
        private DropDownListBLL ddlBLL = new DropDownListBLL();
        private IMasRecordStatusBLL rsBLL = new MasRecordStatusBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();

        //private CommandInfo cmdInfo;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DataItem != null)
            {
                FillDataToRadioJobSiteType();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    rblJobSiteType.SelectedValue = DataBinder.Eval(DataItem, "cmd_flag").ToString();
                    UpdateControlApprover();
                    if (rblJobSiteType.SelectedValue == SystemInfo._JobsiteExternal)
                    {
                        txtAppName.Text = DataBinder.Eval(DataItem, "approved_name").ToString();
                    }
                    else
                    {
                        cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "approved_by").ToString();
                    }
                    DatePicker1.SetValue(DataBinder.Eval(DataItem, "approved_date", "{0:dd/MM/yyyy}").ToString());
                    txtCmdDraftNo.Text = DataBinder.Eval(DataItem, "cmd_draft_no").ToString();
                    txtCmdRefNo.Text = DataBinder.Eval(DataItem, "cmd_ref_no").ToString();
                    cmbCmdStatus.SelectedValue = DataBinder.Eval(DataItem, "cmd_status").ToString();
                }
                else
                {
                    rblJobSiteType.SelectedIndex = 0;
                    UpdateControlApprover();
                    cmbCmdStatus.SelectedValue = SystemInfo._WorkRecordStatus;
                    txtCmdDraftNo.Text = cmdBLL.GetNextDraftNo(SystemInfo._CmdJobSite);
                }
                //rdoInternal.Checked = (cmbEmployee.SelectedValue.ToString() != string.Empty && txtAppName.Text.Trim().Length <= 0);
                //rdoExternal.Checked = !rdoInternal.Checked;
            }

        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected void FillDataToRadioJobSiteType()
        {
            IList<DropDownListInfo> infoList = ddlBLL.LoadListJobsiteTypes();

            rblJobSiteType.DataSource = infoList;
            rblJobSiteType.DataBind();
        }
        public void FillDataToApprover()
        {
            cmbEmployee.Items.Clear();

            DataSet ds = empBLL.LoadEmployeeForApproveCommands();
            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = dt.Rows[i]["EMP_NAME"].ToString();
                item.Value = dt.Rows[i]["EMP_SEQ"].ToString();

                item.Attributes.Add("EMP_NAME", dt.Rows[i]["EMP_NAME"].ToString());
                item.Attributes.Add("EXEC_POS_NAME", dt.Rows[i]["EXEC_POS_NAME"].ToString());

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }

        protected void UpdateControlApprover()
        {
            bool bInternal = rblJobSiteType.SelectedValue == SystemInfo._JobsiteInternal;

            if (bInternal)
            {
                FillDataToApprover();
                txtAppName.Text = "";                
            }
            else
            {
                cmbEmployee.Items.Clear();
            }
            rdoInternal.Checked = bInternal;
            rdoExternal.Checked = !bInternal;
            rdoInternal.Enabled = bInternal;
            rdoExternal.Enabled = !bInternal;
            cmbEmployee.Enabled = bInternal;
            txtAppName.Enabled = !bInternal;
        }
        //============================= ControlEvents ======================================

        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            cmdBLL.Insert(SystemInfo._CmdJobSite
                , rblJobSiteType.SelectedValue
                , txtCmdDraftNo.Text.Trim()
                , txtCmdRefNo.Text.Trim()
                , txtCmdDescr.Text.Trim()
                , cmbCmdStatus.SelectedValue
                , rdoInternal.Checked ? cmbEmployee.SelectedValue.Length > 0 ? Int32.Parse(cmbEmployee.SelectedValue) : (int?)null : (int?)null                
                , txtAppName.Text.Trim()
                , txtApprovePosition.Text.Trim()
                , DatePicker1.GetStringValue().Length > 0 ? DateTime.Parse(DatePicker1.GetStringValue()) : (DateTime?)null
                , ""
                , ssMng.EmpSeqI);
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            cmdBLL.Update(SystemInfo._CmdJobSite
                 , Int32.Parse(lblJobsiteCmdSeq.Text.Trim())
                 , rblJobSiteType.SelectedValue
                 , txtCmdDraftNo.Text.Trim()
                 , txtCmdRefNo.Text.Trim()
                 , txtCmdDescr.Text.Trim()
                 , cmbCmdStatus.SelectedValue
                 , rdoInternal.Checked ? cmbEmployee.SelectedValue.Length > 0 ? Int32.Parse(cmbEmployee.SelectedValue) : (int?)null : (int?)null
                 , txtAppName.Text.Trim()
                 , txtApprovePosition.Text.Trim()
                 , DatePicker1.GetStringValue().Length > 0 ? DateTime.Parse(DatePicker1.GetStringValue()) : (DateTime?)null
                 , ""
                 , ssMng.EmpSeqI);
        }

        protected void cmbEmployee_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (rdoInternal.Checked)
            {
                txtApprovePosition.Text = "";
                if (cmbEmployee.SelectedValue.Length > 0)
                {
                    IList<EmployeeInfo> empList = empBLL.ListEmployees(cmbEmployee.SelectedValue);
                    EmployeeInfo empInfo = new EmployeeInfo();
                    empInfo = empList[0];
                    txtApprovePosition.Text = empInfo.Position.Executive.Name;
                }
            }
        }

        protected void rblJobSiteType_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateControlApprover();
        }
    }
}